package com.xingdata.jjxc.favorites;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.xingdata.jjxc.R;

/**收藏夹adapter*/
public class FavoritesAdapter extends ArrayAdapter<FavoritesEntity>{

	private int resourceId;
	
	public FavoritesAdapter(Context context, int resource, List<FavoritesEntity> objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		resourceId = resource;
	}

	/**另外又重写了 getView()方法，这个方法在每个子项被滚动到屏幕内的时候会被调用*/
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		FavoritesEntity favoritesEntity = getItem(position);//获取当前item子项的实例
		View view;
		/**ViewHolder优化View的findViewById（）方法获取控件实例*/
		ViewHolder viewHolder;
		/*提升listview运行效率，运用convertView参数将之前加载好的布局进行缓存*/
		if (convertView == null) {
			view = LayoutInflater.from(getContext()).inflate(resourceId, null);
			viewHolder = new ViewHolder();
			viewHolder.place_name = (TextView) view.findViewById(R.id.place_name);
			viewHolder.place_address = (TextView) view.findViewById(R.id.place_address);
			view.setTag(viewHolder);//将ViewHolder存储在View中
		}else {
			view =convertView;//如果view中的item已经加载过就直接重用
			viewHolder = (ViewHolder) view.getTag();//重用获取ViewHolder（作用：重新用此方法获取view控件已经获取的实例）
		}
		
		viewHolder.place_name.setText(favoritesEntity.getPlace_name());
		viewHolder.place_address.setText(favoritesEntity.getPlace_address());
		
		return view; 
	}
	
	/*新增内部类ViewHolder(用于对控件的实例进行缓存)*/
	/**用ViewHolder优化每次在 getView()方法中还是会调用 View的 findViewById()方法来获取一次控件的实例*/
	class ViewHolder {
		/**地址标题*/
		TextView place_name;
		/**地址内容*/
		TextView place_address;
	}


}
